﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace YAGMRC.Core.Commands
{
    public class GetGamesPlayersCommand : ICommand, IResult<YAGMRC.Core.Models.GetGamesAndPlayers.GetGamesAndPlayersCommandResult>
    {
        #region Constructor

        private GetGamesPlayersCommand()
        {
        }

        public GetGamesPlayersCommand(Resolver resolver)
        {
            m_Resolver = resolver;
        }

        #endregion Constructor

        private Resolver m_Resolver;

        public void Execute()
        {
            GetGamesForPlayerCommand cmd1 = new GetGamesForPlayerCommand(m_Resolver);
            cmd1.Execute();
            if (!cmd1.Result.HasResult)
            {
                Trace.Fail("GetGamesForPlayerCommand no result");
            }

            m_Resolver.Register<YAGMRC.Core.Models.GetGamesForPlayer.GetGamesForPlayerCommandResult>(cmd1.Result);

            GetGamesAndPlayersCommand cmd2 = new GetGamesAndPlayersCommand(m_Resolver);
            cmd2.Execute();

            if (!cmd2.Result.HasResult)
            {
                Trace.Fail("GetGamesForPlayerCommand no result");
            }

            this.Result = cmd2.Result;

            this.m_Resolver.Register<YAGMRC.Core.Models.GetGamesAndPlayers.GetGamesAndPlayersCommandResult>(this.Result);
        }

        public bool CanExecute()
        {
            AuthenticateCommandResult auth = m_Resolver.Get<AuthenticateCommandResult>();
            if ((null != auth) && (auth.Authenticated))
            {
                return true;
            }

            return false;
        }

        public Models.GetGamesAndPlayers.GetGamesAndPlayersCommandResult Result
        {
            get;
            private set;
        }
    }
}